package org.goldfish.mapreduce.orders;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Order implements Writable {
    String  orderId;
    String  userId;
    int orderAmount;
    String  orderTime;
    OrderKey getOrderKey(){
        return OrderKey.builder()
                .userId(userId)
                .orderTime(orderTime)
                .build();
    }

    @Override
    public void write(DataOutput out) throws IOException {
        Text.writeString(out, orderId == null ? "" : orderId);
        Text.writeString(out, userId == null ? "" : userId);
        out.writeInt(orderAmount);
        Text.writeString(out, orderTime == null ? "" : orderTime);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.orderId = Text.readString(in);
        this.userId = Text.readString(in);
        this.orderAmount = in.readInt();
        this.orderTime = Text.readString(in);
    }
}
